/config,nproc,4
/config,nres,20000
/FILNAME, FSW_Simp
/PREP7
/title, sequentially coupled analysis with heat flux on top surface
Antype,trans

ET,1,SOLID70
MP,DENS,1,2700
MPTEMP,,25,100,150,200,250,300,400,500,600,700,750,800,850,900
MPDATA,KXX,1,,52,51.5,50.5,49.5,48,47,42,38,34,31,29,26.5,26,26.5  
MPDATA,C,1,,480,490,500,515,530,550,600,670,750,800,840,900,970,1050  




length=0.203
tlength=0.175
width=0.101
thick=0.00635
xstart=0.014
nthke=4
smesize=0.001537878

ncopy=NINT(length/(4*smesize))
block,,4*smesize,,thick,,10*smesize
block,,4*smesize,,thick,10*smesize,12*smesize
block,,4*smesize,,thick,12*smesize,14*smesize
block,,4*smesize,,thick,14*smesize,width
nummrg,all
numcmp,all
lesize,1,,,nthke
MSHAPE,0,3D
MSHKEY,1
esize,smesize
vMESH,1
lesize,15,,,2,
lesize,13,,,2,
lesize,14,,,4,
lesize,16,,,4,
lesize,17,,,1,
lesize,20,,,1,
lesize,18,,,1,
lesize,19,,,1,
vMESH,2
lesize,22,,,2,
lesize,24,,,2,
lesize,21,,,2,
lesize,23,,,2,
lesize,25,,,1,
lesize,26,,,1,
lesize,27,,,1,
lesize,28,,,1,
vmesh,3
lesize,36,,,18
vmesh,4
VGEN,ncopy, all,,,4*smesize,,,,,
VSYMM, Z, all
nummrg,all
FINISH

/SOLU 
tunif,25 
allsel
nsel,s,loc,y,
SF,all,CONV,250,20 
allsel
nsel,s,loc,x,0
nsel,a,loc,x,length
nsel,a,loc,z,width
nsel,a,loc,y,thick
SF,all,CONV,30,20
allsel


kbc,1 
time,0.001
DELTIM,0.001 
solve

OUTRES,all,all
AUTOTS,ON
ddept=0.00622
ddtime=0.2
dvel=0.00622/175
dinc=ddtime*dvel
dtime=0.1
dtotal=178
dweltime=2
ndwel=dweltime/dtime
dnloct=dtotal/ddtime 
tvel=0.00042                       !welding speed
xinc=dtime*tvel
nloct=NINT(tlength/xinc)
rshoulder=0.0095
rprobe=0.00395
phinput=291.07
nheatgen=phinput/(dnloct*dinc*3.14*rprobe*rprobe)
ehfulx=654.92

    nsel,s,loc,y,thick
    nsel,r,loc,z,-rprobe,rprobe
    nsel,r,loc,x,-rprobe,rprobe
    ESLN,s,
    SFEDELE,all,4,CONV
allsel
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)
    *get,numnode,node,0,count   !number of nodes in the selected set
    *get,lnnum,node,0,num,min    !loest node number in the selected set
    *dim,nodenumber,ARRAY,numnode 
    *dim,nodenumber2,ARRAY,numnode 
    *dim,nodenumber3,ARRAY,numnode 
    *dim,nodehflux,ARRAY,numnode
    *dim,emnumber,ARRAY,numnode 
    allsel
! plunge
*do,ka,1,dnloct,1
    x0=xstart   !start point
    z0=0
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)

     nnum=lnnum
     kd=0
    *do,kb,1,numnode
       *get,xlocation,node,nnum,loc,x
       *get,zlocation,node,nnum,loc,z
       *get,ylocation,node,nnum,loc,y
        rd=SQRT((xlocation-x0)**2+(zlocation-z0)**2)

          *if,rd,le,rprobe,THEN
            *if,ylocation,ge,(thick-ka*dinc),then
	            kd=kd+1
	            nodenumber2(kd)=nnum
 	            bf,nnum,HGEN,nheatgen
             *endif
          *endif
       *get,nexthn,node,nnum,nxth
        nnum=nexthn
     *enddo
    allsel
        step2time=ka*ddtime+0.001
        time,step2time
        DELTIM,ddtime
        KBC,1 
        outres,BASIC,LAST       
        solve
/solu
    *do,ke,1,kd
     bfdele,nodenumber2(ke),HGEN
    *enddo
*enddo
allsel



*do,i,1,(nloct+ndwel),1
        *if,i,le,ndwel,then
           x0=xstart
        *else
          x0=xstart+(i-ndwel)*xinc   !start point
        *endif
           z0=0
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)

     nnum=lnnum
     jj=0
     kk=0
     tt=0
    *do,ia,1,numnode
       *get,xlocation,node,nnum,loc,x
       *get,zlocation,node,nnum,loc,z
       *get,ylocation,node,nnum,loc,y
        rd=SQRT((xlocation-x0)**2+(zlocation-z0)**2)

          *if,rd,le,rshoulder,and,rd,gt,rprobe,THEN
            *if,ylocation,eq,thick,then
            jj=jj+1
            nodenumber(jj)=nnum
            nodehflux(jj)=3*ehfulx*rd/(2*3.1416*(rshoulder**3-rprobe**3))
            *endif
          *endif
          *if,rd,le,rshoulder,THEN
            *if,ylocation,eq,thick,then
            tt=tt+1
            nodenumber3(tt)=nnum
            *endif
          *endif
          *if,rd,le,rprobe,then
            kk=kk+1
            nodenumber2(kk)=nnum
             bf,nnum,HGEN,nheatgen
          *endif
       *get,nexthn,node,nnum,nxth
        nnum=nexthn
     *enddo
    allsel
     *do,ta,1,tt,1
        *if,ta,eq,1,then
          nsel,s,node,,nodenumber3(ta) 
        *else
         nsel,a,node,,nodenumber3(ta)
        *endif
     *enddo
     ESLN,s,
     SFEDELE,all,4,CONV
    allsel

     *do,ib,1,jj,1
        *if,ib,eq,1,then
          nsel,s,node,,nodenumber(ib) 
        *else
         nsel,a,node,,nodenumber(ib)
         n3=nodenumber(ib)
        *endif
     *enddo
     ESLN,s,

     *get,numelem,elem,0,count
     *get,lenum,elem,0,num,min

     enum=lenum
     nsle,all    
    *do,ii,1,numelem
     emnumber(ii)=enum
     nnumk=nelem(enum,3) 
     nnuml=nelem(enum,4) 
     nnumo=nelem(enum,7) 
     nnump=nelem(enum,8) 
     hfluxl=0
     hfluxk=0
     hfluxp=0
     hfluxo=0
       *do,iii,1,jj
          *if,nnuml,eq,nodenumber(iii),then
              hfluxl=nodehflux(iii)
             *elseif,nnumk,eq,nodenumber(iii)
              hfluxk=nodehflux(iii)
             *elseif,nnump,eq,nodenumber(iii)
              hfluxp=nodehflux(iii)
             *elseif,nnumo,eq,nodenumber(iii)
              hfluxo=nodehflux(iii)
          *endif
       *enddo
       aehflux=(hfluxl+hfluxk+hfluxp+hfluxo)/4
       sfe,enum,4,HFLUX,,hfluxk,hfluxl,hfluxp,hfluxo 

      *get,nexthe,elem,enum,nxth
      enum=nexthe
   *enddo
  allsel
  step3time=step2time+i*dtime          
        time,step3time
        DELTIM,dtime
        KBC,1 
        outres,BASIC,LAST        
        solve
/solu
    *do,id,1,numelem
     enum=emnumber(id)
     SFEDELE,enum,4,hflux
    *enddo
    *do,ik,1,kk
     bfdele,nodenumber2(ik),HGEN
    *enddo
    nsel,s,loc,y,thick
    SF,all,CONV,30,20
    allsel
*enddo


    nsel,s,loc,y,thick
    SF,all,CONV,30,20
    allsel
*do,hhkk,1,376
step5time=hhkk*0.5+step3time
        time,step5time
        DELTIM,0.5
        outres,BASIC,LAST 
        solve
*enddo
step5time=step5time+0.2
        time,step5time
        DELTIM,0.2
	outres,erase
        outres,BASIC,LAST 
        solve
finish

finish
/PREP7
Antype,static
NROPT,UNSYM
ET,1,SOLID45
mp,ex,1, 200e9        
mp,alpx,1,0.000012
mp,nuxy,1,.29
MP,DENS,1,7870
!   
TB,BKIN,1,5               ! Activate a data table
TBTEMP,25                ! Temperature = 25
TBDATA,1,262E6,0           ! Yield = 44,000; Tangent modulus = 0
TBTEMP,300                
TBDATA,1,260E6,0
! TBTEMP,400                
! TBDATA,1,250E6,0 
! TBTEMP,500                
! TBDATA,1,200E6,0
! TBTEMP,600                
! TBDATA,1,110E6,0
! TBTEMP,650                
! TBDATA,1,75E6,0 
! TBTEMP,700                
! TBDATA,1,50E6,0
TBTEMP,800                
TBDATA,1,30E6,0
TBTEMP,900                
TBDATA,1,15E6,0   
!   
nsel,all
nsel,s,loc,z,width
nsel,a,loc,z,-width
nsel,r,loc,y,0
d,all,all,0
nsel,all
WPOFFS, xstart, thick
WPROTA,,-90
CYLIND,,rshoulder,,4*thick,0,360, 
WPROTA,,90
WPOFFS, -xstart, -thick
MP,MU,1,0.3
MAT,1   
R,3 
REAL,3  
ET,2,170
ET,3,174
KEYOPT,3,9,0
KEYOPT,3,10,2   
R,3,
RMORE,  
RMORE,,0
RMORE,0 
! Generate the target surface   
asel,s,loc,y,thick
asel,r,loc,z,-4*smesize,4*smesize
CM,_TARGET,AREA 
AATT,-1,3,2,-1  
TYPE,2  
AMESH,ALL   
allsel
! Create a pilot node   
N,40000, xstart,thick,0  
TSHAP,PILO  
E,40000
! Generate the contact surface  
asel,s,loc,y,thick
asel,r,loc,z,-10*smesize,10*smesize
asel,u,,,26
CM,_CONTACT,AREA
TYPE,3  
NSLA,S,1
ESLN,S,0
ESURF   
ALLSEL 
block,-4*smesize,length+4*smesize,-smesize,,-width-4*smesize,width+4*smesize
/COM, CONTACT PAIR CREATION - START 
MP,MU,1,0.3 
MAT,1   
R,4 
REAL,4  
ET,4,170
ET,5,174
KEYOPT,5,9,0
KEYOPT,5,10,2   
R,4,
! Generate the target surface   
ASEL,S,,,62
CM,_TARGET,AREA 
AATT,-1,4,4,-1  
TYPE,4  
AMESH,ALL   
! Create a pilot node   
N,80000, 0,0,0  
TSHAP,PILO  
E,80000
! Generate the contact surface  
allsel
ASEL,S,loc,y,0
ASEL,U,,,62
CM,_CONTACT,AREA
TYPE,5  
NSLA,S,1
ESLN,S,0
ESURF   
*SET,_REALID,4  
ALLSEL  
ESEL,ALL
/PNUM,TYPE,1
/NUM,1  
EPLOT   
/COM, CONTACT PAIR CREATION - END   

finish
/solu
D,40000,UZ,0
D,40000,UX,0
D,40000,ROTX,0
D,40000,ROTZ,0
d,80000,all,0
NLGEOM,on
AUTOTS,on
tref,25
ldread,temp,1,,,,,rth  ! Read in temperatures from thermal run
time,0.001
outres,erase
OUTRES,BASIC,last    
solve
!                        plunging stage
rsteps=dnloct+1
*do,iijj,2,rsteps
           *if,iijj,eq,rsteps,then
F,40000,FY,-18700,
           *else
F,40000,FY,-(17500/175)*(iijj-1)*ddtime,
           *endif 
D,40000,ROTY,50*ddtime*(iijj-1),
ldread,temp,iijj,,,,,rth  ! Read in temperatures from thermal run
step2time=(iijj-1)*ddtime+0.001
time,step2time
DELTIM,ddtime/20,ddtime/10000,ddtime
outres,erase
OUTRES,BASIC,last  
kbc,0       
solve
*enddo
!                      dweling and traverse
rsteps=rsteps+nloct+ndwel
*do,iikk,1,(nloct+ndwel),1
        *if,iikk,le,ndwel,then
		D,40000,UX,0
        *else
                D,40000,UX,(iikk-ndwel)*xinc  
        *endif
     F,40000,FY,-18700,,
D,40000,ROTY,(50*dtime*iikk+50*ddtime*(iijj-1)),
ldread,temp,(iikk+iijj),,,,,rth  ! Read in temperatures from thermal run
step3time=iikk*dtime+step2time
time, step3time
DELTIM,dtime/20,dtime/10000,dtime
outres,erase
OUTRES,BASIC,last
kbc,0     
solve
*enddo

F,40000,FY,0 
D,40000,UX,nloct*xinc 
D,40000,UY,10*xinc,

rsteps=iikk+iijj
allsel
*do,hhii,1,376
step5time=hhii*0.5+step3time
ldread,temp,(rsteps+hhii),,,,,rth  ! Read in temperatures from thermal run
        time,step5time
        DELTIM,0.5,0.01,5
outres,erase
OUTRES,NSOL,last   
OUTRES,STRS,last 
        kbc,0
        solve
*enddo
save
kh=rsteps+hhii
nsel,all
nsel,s,loc,z,width
nsel,a,loc,z,-width
nsel,r,loc,y,0
DDELE,all,all
allsel
nsel,s,loc,x,0
nsel,r,loc,y,thick
nsel,r,loc,z,width
d,all,all,0
allsel
*SET,step5time,0.2+step5time
ldread,temp,(kh+1),,,,,rth  ! Read in temperatures from thermal run 
time,step5time  
DELTIM,0.005,0.001,5 
outres,erase
OUTRES,BASIC,last   
kbc,0   
solve

finish
save